
import java.io.IOException;
import java.util.Random;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;

public class GameManager extends LayerManager {

    final int WOOD = 2, METAL = 3, FLOOR = 1;
    boolean up = true;
    String fileName = "";
    int[][] map;
    Board board;
    BomberMan bomber;
    int bomber_size = 20;
    final int RIGHT = 240, TOP = 51, LEFT = 0, BOTTOM = 220;
    int board_x = 0;
    int board_y = 51;
    Monster[] monsters = new Monster[3];
    Bomb[] bomb = new Bomb[3];

    public GameManager() throws IOException, Exception {
        // TODO Auto-generated constructor stub
        map = new int[][]{
            {FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR},
            {FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR},
            {FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR},
            {FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR, FLOOR}};
        Image myImg = Image.createImage("/resource/newTiles.png");
        System.out.println(myImg.getHeight());
        board = new Board(map[0].length, map.length, myImg, 20, 20);
        board.setPosition(board_x, board_y);
        // bomber
        bomber = new BomberMan(0, 0);
        bomber.setPosition(0, 51);
        // monster
        int j = 0;
        for (int i = 0; i < monsters.length; i++) {
            j++;


            System.out.println(i + " monsters Created");
            boolean validPos = false;
            int posX = 0;
            int posY = 0;
            while (!validPos) {
                Random number = new Random();
                number.setSeed(System.currentTimeMillis());

                posX = Math.abs(number.nextInt()) % map[0].length;
                posY = Math.abs(number.nextInt()) % map.length;

                if (posX < map[0].length && posY < map.length && map[posY][posX] == FLOOR) {
                    validPos = true;
                }
            }
            monsters[i] = new Monster(posX, posY);

            monsters[i].setPosition(posX * 20, (posY + 51) * 20);
            this.append(monsters[i]);

        }
        // append

        this.append(bomber);
        this.append(board);

    }

    public void draw(Graphics g) throws IOException {
        g.drawImage(Image.createImage("/resource/map.png"), 0, 0, Graphics.TOP
                | Graphics.LEFT);
        board.setPosition(0, 51);
        drawCells();

    }

    public void drawCells() {
        int width = map[0].length;
        int height = map.length;
        board.setPosition(0, 51);
        // this.setViewWindow(0, 50, 20 * map[0].length, 20 * map.length);
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                board.setCell(j, i, map[i][j]);
            }
        }

        this.append(board);

    }

    public void move() {
        bomber.move();
        for (int i = 0; i < monsters.length; i++) {
            monsters[i].move();
        }

        MonsterMove();
        for (int i = 0; i < bomb.length; i++) {

            if (bomb[i] != null) {
                if (bomb[i].Active) {
                    if (bomb[i].time == 0) {
                        bomb[i].time = -1;
                        //	System.out.println("time <= 0");
                        // explode
                        int index_x = Round(bomb[i].getX()) / 20;
                        int index_y = Round(bomb[i].getY() - 51) / 20;

                        System.out.println("index x " + index_x + " index y"
                                + index_y);
                        // check right

                        System.out.println(map[index_y][index_x]);

                        // check right
                        if (map[index_y][index_x + 1] == WOOD) {
                            map[index_y][index_x + 1] = FLOOR;
                        }
                        // check left
                        if (map[index_y][index_x - 1] == WOOD) {
                            map[index_y][index_x - 1] = FLOOR;
                        } // check Up
                        if (map[index_y - 1][index_x] == WOOD) {
                            map[index_y - 1][index_x] = FLOOR;
                        } // check down
                        if (map[index_y + 1][index_x] == WOOD) {
                            map[index_y + 1][index_x] = FLOOR;
                        }

                    }
                    try {
                        bomb[i].move();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }

    public int Round(int position) {
        int target_position = 0;
        int div = position / 20;
        int mod = position % 20;
        if (mod > 10) {
            target_position = div * 20 + 20;
        } else {
            target_position = div * 20;
        }
        return target_position;
    }

    public void PlaceBomb() throws Exception {
        for (int i = 0; i < bomb.length; i++) {

            if (bomb[i] == null || !bomb[i].Active) {
                System.out.println("Bomb no " + i + "Created");
                int x = Round(bomber.getX());
                int y = Round(bomber.getY() - 51);

                bomb[i] = new Bomb(x, y);

                bomb[i].setPosition(x, y + 51);

                bomb[i].Activate();
                this.append(bomb[i]);
                bomb[i].setVisible(true);
                break;
            } else {
            }
        }
    }

    public void MonsterMove() {
        for (int i = 0; i < monsters.length; i++) {
//			if (monsters[i].speed_y == TOP) {
//				up = false;
//			} else if (monsters[i].speed_y == 187) {
//				up = true;
//			}
            up = false;
            monsters[i].Move(true, up);

        }
    }

    public void Quite() {
        bomber.StopMove();
    }

    public void left() {
        if (bomber.getX() % 170 == 0) {
            move_board_left();
        }
        int i = bomber.speed_x / 20;
        int j = (bomber.speed_y - 51) / 20;
        int im = bomber.speed_x % 20;
        int jm = (bomber.speed_y - 51) % 20;
        if (bomber.speed_x == 0 || im == 0) {
            if (jm == 0 && map[j][i - 1] == FLOOR) {
                bomber.left();
            } else {
                bomber.StopMove();
            }
        } else {
            bomber.left();
        }


    }

    public void Right() {

//        if (bomber.getX() >= map[0].length * 20) {
//        } else {
//            bomber.right();
//        }
        if (bomber.getX() != 0) {
            if (bomber.getX() % 170 == 0) {
                //   System.out.println("enta 3abeet ya 3abeet " + bomber.getX());
                move_board_right();
            }
        }
        System.out.println("Righttttttttttttttttttttttttt" + bomber.getX());
        int i = bomber.getX() / bomber_size;
        int j = (bomber.getY() - 51) / bomber_size;
        int im = bomber.getX() % bomber_size;
        int jm = (bomber.getY() - 51) % bomber_size;
        if (bomber.getX() == 0 || im == 0) {
            if (jm == 0 && map[j][i + 1] == FLOOR) {
                bomber.right();
            } else {
                bomber.StopMove();
            }
        } else {
            bomber.right();
        }

    }

    public void Up() {
        // System.out.println("UpPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP"+bomber.getY());

        if (bomber.getY() % 181 == 0) {
            //                   System.out.println("UpPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP");

            move_board_up();
        }
//        bomber.up();
//        System.out.println("UpPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP");
        int i = bomber.speed_x / bomber_size;
        int j = (bomber.speed_y - 51) / bomber_size;
        int im = bomber.speed_x % bomber_size;
        int jm = (bomber.speed_y - 51) % bomber_size;


        if (bomber.speed_y == 0 || jm == 0) {
            if (im == 0 && map[j - 1][i] == FLOOR) {
                bomber.up();
            } else {
                bomber.StopMove();
            }
        } else {
            bomber.up();
        }

    }

    public void Down() {
        System.out.println("GET Y : " + bomber.getY());
//        bomber.down();
        if (bomber.getY() % 181 == 0) {
            move_board_down();
        }
        int i = bomber.speed_x / bomber_size;
        int j = (bomber.speed_y - 51) / bomber_size;
        int im = bomber.speed_x % bomber_size;
        int jm = (bomber.speed_y - 51) % bomber_size;
        System.out.println("Downnnnnnnnnnnnnn+ " + i + "y        x" + j);
        if (bomber.speed_y == 0 || jm == 0) {
            if (im == 0 && map[j + 1][i] == FLOOR) {
                bomber.down();
            } else {
                bomber.StopMove();
            }
        } else {
            bomber.down();
        }



    }

    public void move_board_down() {
        if (bomber.getY() >= 20 * map.length) {
            //don't move
        } else {
//            board_x =20;
            System.out.println("board x " + bomber.getY());
            int temp = ((int) Math.ceil(bomber.getX() / 170));
            System.out.println("Da5lt move board down with y = " + temp);

            this.setViewWindow(temp * 170, bomber.getY(), Integer.MAX_VALUE, Integer.MAX_VALUE);
        }
    }

    public void move_board_up() {
        if (bomber.getY() != 51 || bomber.getY() % 181 == 0) {
            //don't move

            System.out.println("Da5lt move board down with y = " + bomber.getY());
//            board_x =20;
            int temp = ((int) Math.ceil(bomber.getX() / 170));
            System.out.println("board x " + bomber.getY());
            this.setViewWindow(temp * 170, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
        }
    }

    public void move_board_right() {
        if (bomber.getX() >= 20 * map[0].length) {
            //don't move
        } else {
            System.out.println("Da5lt");
//            board_x =20;
            System.out.println("board x " + bomber.getX());
            this.setViewWindow(bomber.getX(), 0, Integer.MAX_VALUE, Integer.MAX_VALUE);


        }
    }

    public void move_board_left() {
        if (bomber.getX() == 0) {
        } else if (bomber.speed_x % 170 == 0) {
            System.out.println("d5lt move board with x = " + bomber.speed_x);

            System.out.println("board x " + bomber.speed_x);
            this.setViewWindow(bomber.speed_x - 170, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
            //don't move
        } else {
        }
    }
      public String getEquation() {
        Random number = new Random();
        number.setSeed(System.currentTimeMillis());
        int first_num_1 = number.nextInt(9);
        first_num_1 = number.nextInt(9);
        int first_num_2 = number.nextInt(9);
        int second_num_1 = number.nextInt(9);
        second_num_1 = number.nextInt(9);
        int second_num_2 = number.nextInt(9);

        String operation = "";
        int result = 0;
        int op = number.nextInt(2);
        int first_num = (first_num_1 + first_num_2 * 10);
        int second_num = (second_num_1 + second_num_2 * 10);
        if (op == 0) {
            operation = "+";
            result = first_num + second_num;
        } else if (op == 1) {
            operation = "-";
            result = first_num - second_num;
        } else {
            operation = "*";
            result = first_num - second_num;
        }

        return first_num + operation + second_num + "=" + result;
    }
}